#include <QtGui>

#include "song_widget.h"

song_widget::song_widget(QWidget *parent, QString song_path)
        : QWidget(parent)
{
    ui.setupUi(this);
    codec = QTextCodec::codecForName("UTF8");
    QTableWidgetItem *temp_item1 = new QTableWidgetItem();
    QTableWidgetItem *temp_item0 = new QTableWidgetItem();
    ui.song_selector_table->setSortingEnabled(0);
    //song_path=QString("./songs");

    ///load song names and numbers
    QDir dir;
    dir.cd(song_path);
    dir.setFilter(QDir::Files | QDir::NoSymLinks);
    QStringList filters;
    filters << "*.s.txt";
    dir.setNameFilters(filters);
    //dir.setSorting(QDir::Size | QDir::Reversed);

    QFileInfoList list = dir.entryInfoList();
    ui.song_selector_table->setRowCount(list.size());
    for (int i = 0; i < list.size(); ++i)
    {
        QFileInfo fileInfo = list.at(i);
        QStringList list1 = fileInfo.fileName().split(".");
        QFile file(QString(fileInfo.absolutePath ()+"/"+fileInfo.fileName()));
        file.open( QIODevice::ReadOnly );
        temp_item0->setText(QString("%1").arg(list1.at(0),4));
        ui.song_selector_table->setItem(i, 0,temp_item0->clone());
        temp_item1->setText(codec->toUnicode(file.readLine()));
        ui.song_selector_table->setItem(i, 1,temp_item1->clone());
        //fileInfo.fileName();
        song_path=QString(fileInfo.absolutePath ()+"/");
        file.close();
    }
    ui.song_selector_table->verticalHeader()->hide();
    ui.song_selector_table->resizeColumnToContents (0);
    ui.song_selector_table->setColumnWidth(1,250);
    ui.song_selector_table->sortByColumn (1,Qt::AscendingOrder);
    ui.song_selector_table->setSortingEnabled(1);
    ///////////////////////////////////////////////////////////////
    global_song_path=song_path;

}


void song_widget::on_add_to_playlist_released()
{
    int row=ui.song_selector_table->currentRow();
    if (row>=0){
        QString text = ui.song_selector_table->item(row,0)->text() +"  "+ ui.song_selector_table->item(row,1)->text();
        ui.playlist->addItem(text);
        ui.playlist->setCurrentRow(ui.playlist->count()-1);
        ui.playlist->setFocus();
    }
}

void song_widget::on_song_selector_table_cellDoubleClicked(int x, int y)
{
    on_add_to_playlist_released();
}





void song_widget::on_remove_from_playlist_released()
{
    ui.playlist->takeItem(ui.playlist->currentRow());
}

void song_widget::on_playlist_itemDoubleClicked(QListWidgetItem *current_item)
{
    QString text;
    text=current_item->text();
//	text=text.toInt(text.left(4));
    load_song(text.left(4).toInt());
    //ui.song_lcd->display(text.left(4).toInt());
    //load_song(1);

}

void song_widget::on_song_spinbox_valueChanged(int new_value)
{
    ui.song_selector_table->sortByColumn (0,Qt::AscendingOrder);
    ui.song_selector_table->setCurrentCell (new_value-1,0);

}




void song_widget::on_verse_list_currentRowChanged(int new_row)
{
    ui.verse_list->scrollToItem(ui.verse_list->item(new_row),QAbstractItemView::PositionAtCenter);
    if (ui.sync_button->isChecked())emit show_verse(ui.verse_list->currentItem()->text()," ");
}



void song_widget::on_verse_list_itemDoubleClicked(QListWidgetItem *current_item)
{
    emit show_verse(current_item->text()," ");
}

void song_widget::on_clear_button_released()
{

    emit show_verse(QString(" "),QString(" "));
}
void song_widget::on_show_button_released()
{
    if (ui.verse_list->count())emit show_verse(ui.verse_list->currentItem()->text()," ");
}

void song_widget::on_fast_button_released()
{
    ui.sync_button->setChecked(1);
    load_song(ui.song_spinbox->value());

}

void song_widget::load_song(int number)
{
    //QStringList list;
    QString	text;
    QString chorus;
    bool has_chorus=0;
    int i;
    i=0;
    ui.song_lcd->display(number);
    QFile file(QString(global_song_path+QString::number(number)+".s.txt"));
    if (file.open( QIODevice::ReadOnly ))
    {
        int sync;
        sync=ui.sync_button->isChecked();
        ui.sync_button->setChecked(0);
        ui.verse_list->clear();
        while (!file.atEnd())
        {
            text=codec->toUnicode(file.readLine());
            text.chop(1);
            if (i==1){chorus=text;if (chorus.size()>3)has_chorus=1;}
            if (i>1)ui.verse_list->addItem(text);
            if (has_chorus && i>1)ui.verse_list->addItem(chorus);
            i++;
        }

        file.close();
        ui.verse_list->setCurrentRow(0);
        ui.verse_list->setFocus();
        ui.sync_button->setChecked(sync);
        if (sync)on_show_button_released();
    }
}
